package com.bm.server.serverImpl;

import com.bm.dao.Neo4jUserDao;
import com.bm.dao.Neo4jUserRepository;
import com.bm.pojo.requestMsg.RequestLoginMsg;
import com.bm.pojo.user.User;
import com.bm.server.UserServer;

import org.springframework.stereotype.Service;



import javax.annotation.Resource;


/**
 * Created with IntelliJ IDEA.
 * User: sx
 * Date: 2022/1/26
 * Time: 17:39
 * Description: java
 */
@Service
public class UserServerImpl implements UserServer {
    @Resource
    private Neo4jUserRepository neo4jRepository;


    @Override
    public RequestLoginMsg loginUI(String account, String password) {
        User user = neo4jRepository.queryByAccountAndPassword(account, password);
        User user1 = neo4jRepository.queryByAccount(account);
        RequestLoginMsg requestLoginMsg = new RequestLoginMsg();
        if(user1!=null && !user1.getPassord().equals(password)){

            requestLoginMsg.setCode("201");
            requestLoginMsg.setMsg("密码错误！");
            return requestLoginMsg;
        }
        if(user==null){
            requestLoginMsg.setCode("202");
            requestLoginMsg.setMsg("账户不存在,请注册！");
            return requestLoginMsg;
        }


        requestLoginMsg.setCode("200");
        requestLoginMsg.setMsg("登录成功！");
        requestLoginMsg.setUser(user);

        return requestLoginMsg;
    }

    @Override
    public RequestLoginMsg signUI(User user) {
        RequestLoginMsg requestLoginMsg = new RequestLoginMsg();

        User account = neo4jRepository.queryByAccount(user.getAccount());
        if(account != null && !"".equals(account.getAccount())){
            requestLoginMsg.setMsg("账户已存在！");
            requestLoginMsg.setCode("500");
        }else {
            User save = neo4jRepository.save(user);
            if(save!=null){
                requestLoginMsg.setMsg("注册成功！");
                requestLoginMsg.setCode("200");
                requestLoginMsg.setUser(save);
            }
        }
        return requestLoginMsg;
    }
}
